package com.wangshidai.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshidai.entity.R;
import com.wangshidai.entity.SysRole;
import com.wangshidai.form.rolefrom.AlterRoleFrom;
import com.wangshidai.form.rolefrom.InsertRoleFrom;
import com.wangshidai.form.rolefrom.QueryRoleNameFrom;
import com.wangshidai.reply.rolereply.QueryRoleNameReply;
import com.wangshidai.service.SysRoleService;
import com.wangshidai.util.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理接口")
public class RoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @PostMapping("/queryRoleName")
    @ApiOperation("根据角色名模糊查询")
    public R queryRoleName(  @RequestBody @Valid QueryRoleNameFrom queryRoleName){
        log.info("接收到前端按角色名模糊查询角色的请求，参数{}",queryRoleName);
        Page<QueryRoleNameReply> page = new Page();
        BeanUtil.copyProperties(queryRoleName,page);
        if(sysRoleService.queryRoleName(page,queryRoleName.getName())){
            return new R(200,"查询成功",page);
        }
        return new R(-200,"未查询到数据",null);
    }
    @PostMapping("/insertRole")
    @ApiOperation("添加角色")
    public R insertRole(@RequestBody @Valid InsertRoleFrom insertRole) throws BusinessException {
        log.info("接收到前端插入角色的请求，参数{}",insertRole);
        SysRole role = new SysRole();
        BeanUtils.copyProperties(insertRole,role);
        if(sysRoleService.insertRole(role)){
            return new R(200,"添加成功",null);
        }
        return new R(-200,"添加失败",null);
    }
    @PutMapping("/alterRole")
    @ApiOperation("根据id修改角色信息")
    public R alterRole(@RequestBody @Valid AlterRoleFrom alterRole) throws BusinessException {
        log.info("接收到前端修改角色请求，参数{}",alterRole);
        SysRole role = new SysRole();
        BeanUtils.copyProperties(alterRole,role);
        if(sysRoleService.alterRole(role)){
            return new R(200,"修改成功",null);
        }
        return new R(-200,"修改失败",null);
    }
    @DeleteMapping("/deleteRoleById/{roleId}")
    @ApiOperation("根据id删除角色")
    public R deleteRoleById(@PathVariable @ApiParam("角色id") long roleId) {
        log.info("接收到前端按角色主键删除角色的请求，参数{}",roleId);
        if(sysRoleService.deleteRoleById(roleId)){
            return new R(200,"删除成功",null);
        }
        return new R(-200,"未查询到该用户",null);
    }
}

